# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：deal_email.py
#日期：2020-06-15
#备注：发送邮件
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from pycacho.cachobase.base_config import sender, passwd
from pycacho.cachobase.file_deal import get_file


# subject-邮件主题、content-邮件正文、file-附件、receivers-收件人（list）、cc_list-抄送人(list)
def send_email(subject,content,file,receivers,cc_list):
    msg = MIMEMultipart('mixed')                     # 设置总的邮件体对象，对象类型为mixed(正文、html、附件等)
    # 文字内容
    text_sub = MIMEText(content, 'plain', 'utf-8')
    msg.attach(text_sub)
    # 附件
    attachment = MIMEText(get_file(file), 'base64', 'utf-8')   # 二进制形式读取
    attachment["Content-Type"] = 'application/octet-stream'
    attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
    msg.attach(attachment)

    #将邮件的主题等相关信息添加到邮件实例
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ";".join(receivers)
    msg['Cc'] = ";".join(cc_list)
    mail_smtpserver = 'smtp.qq.com'
    smtp = smtplib.SMTP()
    smtp.connect(mail_smtpserver)
    smtp.login(sender, passwd)
    smtp.sendmail(sender, receivers, msg.as_string())
    print(subject,'邮件发送成功')


if __name__ == '__main__':
    subject = '中文标题'
    content = '这是我使用python smtplib模块和email模块自动发送的邮件'  # 正文
    file = 'E:\zenglingwei\statistics\\中移动对账20200611.xls'
    receivers = ["zenglw@rendongholdings.com"]      # zhuxl@rendongholdings.com
    send_email(subject,content,file,receivers,[])

